package leetcode;

public class MedianTwoSortedArrays {
	
	public static void main(String[] args) {
		int[] nums1 = {1, 2};
		int[] nums2 = {3, 4};
		System.out.println(findMedianSortedArrays(nums1, nums2));
	}
	public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
		int[] merge;
		if(nums1 == null){
			merge = nums2;
		}else if(nums2 == null){
			merge = nums1;
		}else{
			merge = new int[nums1.length + nums2.length];
		}
		int j = 0, k = 0;
		for (int i = 0; i < merge.length; i++) {
			System.out.println("i: " + i + "  merge " + merge.length);
			if(j >= nums1.length && k < nums2.length){
				 merge[i] = nums2[k++];
				 continue;
			}
			if(k >= nums2.length && j < nums1.length){
				merge[i] = nums1[j++];
				continue;
			}
			merge[i] = nums1[j] < nums2[k] ? nums1[j++] : nums2[k++];
		}
//		for (int i = 0; i < merge.length; i++) {
//			System.out.print("  " + merge[i]);
//		}
		double result = 0;
		if((merge.length & 1) == 1){
		    result = merge[merge.length / 2];
		}else{
			result = (double)(merge[merge.length / 2] + merge[merge.length / 2 - 1]) / 2;
		}
		return result;
	}
}
